﻿
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Aribaccio.Core;
using Aribaccio.Logger;
using BDPAutodetect.Mgrcore;
using BDPAutodetect.Models;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace BDPAutodetect.Client
{
    /// <summary>
    /// 主窗体窗体视图模型类
    /// </summary>
    public partial class MainWindowModel : MenuViewModel<Window>
    {
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public MainWindowModel(UserManager argUserManager, RolePowerManager argRolePowerManager)
            : base(argUserManager, argRolePowerManager)
        {
            //测试菜单
            //this.PowerMenus.Add(new MPowerMenu(1, "主页", "MainPage", "\xe697"));
            //this.PowerMenus.Add(new MPowerMenu(1, "记录", "Record", "\xe810"));
            //this.PowerMenus.Add(new MPowerMenu(1, "参数", "Parameter", "\xe703"));
            //this.PowerMenus.Add(new MPowerMenu(1, "设置", "Setting", "\xe60e"));
        }

        /// <summary>
        /// 窗体最小化操作事件
        /// </summary>
        [RelayCommand]
        public void ExecuteWindowMin()
        {
            if (this.Parent == null) return;
            this.Parent.WindowState = WindowState.Minimized;
        }

        /// <summary>
        /// 窗体最大化操作事件
        /// </summary>
        [RelayCommand]
        public void ExecuteWindowMax()
        {
            if (this.Parent == null) return;
            this.Parent.WindowState = this.Parent.WindowState == WindowState.Maximized
                ? WindowState.Normal
                : WindowState.Maximized;
        }

        /// <summary>
        /// 窗体关闭操作事件
        /// </summary>
        [RelayCommand]
        public void ExecuteWindowClose()
        {
            if (this.Parent == null) return;
            if (MessageBox.Show("您确定退出？", "警告", MessageBoxButton.YesNo) != MessageBoxResult.Yes) return;
            this.Parent.Close();
            Application.Current.Shutdown();
        }

        /// <summary>
        /// 窗体拖拽操作事件
        /// </summary>
        [RelayCommand]
        public void ExecuteDragMove()
        {
            if (this.Parent == null) return;
            this.Parent.DragMove();
        }

        /// <summary>
        /// 导航切换操作
        /// </summary>
        /// <typeparam name="TData">泛型数据模型</typeparam>
        /// <param name="argParameter">相对Url路径名称</param>
        /// <param name="argDataModel">传输数据实例</param>
        public async Task MenuChanged<TData>(string argParameter, TData argDataModel) where TData : class, new()
        {
            var pMenu = this.PowerMenus.FirstOrDefault(v => v.Url != null && v.Url.Equals(argParameter, StringComparison.OrdinalIgnoreCase));
            if (pMenu == null) return;

            var resultTaskTransfer = GetMenuMainContent(pMenu);
            await resultTaskTransfer.ConfigureAwait(false);
            if (resultTaskTransfer == null || resultTaskTransfer.Result == null) return;
            //页面跳转传输数据
            resultTaskTransfer.Result.TransferData<TData>(argDataModel);
        }       
    }
}
